<?php
 
defined("_VALID_ACCESS") || die('Direct access forbidden');
 
class SCTV_MaterialCommon extends ModuleCommon {
 
	public static $display = null;
	
    public static function menu() {
    	return array(__('SCTV') => array(
    		'__submenu__' => 1, __('List') => array(
    			'__submenu__' => 2, 
        		__('Material') => array(),
			)
		));
    }
	public static function material_format_display($record) {
		$temprecord = $record;
		
		if(self::$display == $temprecord) return null;
		if (is_numeric($record)) $record = Utils_RecordBrowserCommon::get_record('SCTV_Material', $record);
		self::$display = $temprecord;
			
		if (!$record) return null;
		
		return $record['material_code'] . ' - ' . $record['material_name']; 	
	}
	public static function material_format($record) {
		if (is_numeric($record)) $record = Utils_RecordBrowserCommon::get_record('SCTV_Material', $record);
		
		if (!$record) return null;
		
		return $record['material_code'] . ' - ' . $record['material_name']; 	
	}
	public static function material_suggestbox($str)
	{
		$str = trim($str);
		$str = DB::Concat(DB::qstr('%'), DB::qstr($str), DB::qstr('%'));
		$recs = Utils_RecordBrowserCommon::get_records('SCTV_Material', array('(~"material_code'=>$str,'|~"material_name'=>$str), array(), array('material_code'=>'ASC'), 10);
		$ret = array();
        foreach($recs as $v) {
            $ret[$v['id']] = self::material_format($v);
        }
        return $ret;
	}
	public static function display_current_price($record, $nolink, $desc)
	{
		$price_istvbox = self::get_current_price_istvbox($record['id'], date('Y-m-d'));
		return Utils_CurrencyFieldCommon::format($price_istvbox['price']);
	}
	public static function display_price($record, $nolink, $desc)
	{
		return Utils_CurrencyFieldCommon::format($record[$desc['id']]);
	}
	public static function get_current_price_istvbox($material, $acceptance_date)
	{
		$recs = Utils_RecordBrowserCommon::get_records('SCTV_MaterialPrice', 
			array(
				'material'=>$material,
				'<=from_date'=>$acceptance_date
			),
			array(), array('from_date'=>'DESC'), 1
		);
		
		if(empty($recs))
			return array('price'=>0, 'istvbox'=>false);
		else
		{
			foreach ($recs as $rec) {
				$istvbox = Utils_RecordBrowserCommon::get_value('SCTV_Material', $material, 'istvbox');
				return array('price'=>$rec['price'], 'istvbox'=>$istvbox?'true':'false');	
			}			
		}
	}
}
 
?>